﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MutiCodecO.codeclib
{
    class CodecGenerater
    {
        public enum CodecIndex : int
        {
            ASII2NUM = 0,
            ASII2HEX,
            ASII2BASE64,
            ASII2URL,
            NUM2ASII,
            HEX2ASII,
            BASE642ASII,
            URL2ASII,

            CODECNUM
        }
        private ICodec[] codecs = 
        {
            new Asii2Num(),
            new Asii2Hex(),
            new Asii2Base64(),
            new Asii2Url(),
            new Num2Asii(),
            new Hex2Asii(),
            new Base642Asii(),
            new Url2Asii()
        };

        public static string[] codecName =
        {
            "字符转十进制数",
            "字符转十六进制数",
            "BASE64编码",
            "URL编码",
            "十进制数转字符",
            "十六进制数转字符",
            "BASE64解码",
            "URL解码"
        };

        public String parse(String strInput)
        {
            if (m_codecs.Length <= 0)
                return strInput;
            String strOutput = strInput;
            int lastCodec = m_codecs.Length - 1;
            while (lastCodec >= 0 && (m_codecs[lastCodec] < 0 || m_codecs[lastCodec] >= (int)CodecIndex.CODECNUM))
                lastCodec--;
            for (int i = 0; i < lastCodec; i++)
            {
                if (m_codecs[i] >= 0 && m_codecs[i] < (int)CodecIndex.CODECNUM)
                {
                     strOutput = codecs[m_codecs[i]].parse(strOutput);
                }
            }
            if (is_encoded)
                strOutput = codecs[m_codecs[lastCodec]].parse(strOutput, codecs[m_codecs[lastCodec]].parse(m_prefix), codecs[m_codecs[lastCodec]].parse(m_partition));
            else
                strOutput = codecs[m_codecs[lastCodec]].parse(strOutput, m_prefix, m_partition);
            return strOutput;
        }

        private int[] m_codecs;

        public void setCodecs(int[] codecs)
        {
            m_codecs = codecs;
        }

        private String m_prefix = "";
        private String m_partition = "";
        private bool is_encoded = false; 

        public void setPrefix(String prefix)
        {
            m_prefix = prefix;
        }

        public void setPartion(String partion)
        {
            m_partition = partion;
        }

        public void makePrefixPartionEncoded()
        {
            is_encoded = true;
        }
    }
}
